﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using BSCMFF.Web.UserControls.Details;
using BSCMFF.Core.Exceptions.Database;
using BSCMFF.Core;

namespace BSCMFF.Web.Pages.BSC_Setup
{
    public partial class BusinessTargetManagement : MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        

        protected void RadGridStrategicTargets_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            BSCEntities context = new BSCEntities();

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "StrategicTargetDetails")
            {//save strategic target
                StrategicTargetDetails form = (StrategicTargetDetails)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);


                int stID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Strategic_Target_ID"]);

                Strategic_Target st = (from sts in context.Strategic_Target
                                       where sts.Strategic_Target_ID == stID
                                       select sts).SingleOrDefault();

                st.Name = form.Name;
                st.Description = form.Description;
                st.Perspective_FK = form.Perspective_FK;
                st.ExpectedDate = form.ExpectedDate;
                                
                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't update target: " + ex.Message);
                }
            }

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "BusinessSolutionDetails")
            {//save business solution
                BusinessSolutionDetails form = (BusinessSolutionDetails)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);


                int bsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ID"]);

                Business_Solution bs = (from bss in context.Business_Solution
                                        where bss.ID == bsID
                                        select bss).SingleOrDefault();

                bs.Solution = form.Solution;

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't update solution: " + ex.Message);
                }
            }

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "KPIDetails_new")
            {//save kpi
                KPIDetails_new form = (KPIDetails_new)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);


                int kpiID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["KPI_ID"]);

                KPI kpi = (from kpis in context.KPIs
                           where kpis.KPI_ID == kpiID
                           select kpis).SingleOrDefault();

                kpi.Name = form.Name;
                kpi.Description = form.Description;
                kpi.Perspective_FK = form.Perspective_FK;
                kpi.Measure = form.Measure;

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't update KPI: " + ex.Message);
                }
            }
        }

        protected void RadGridStrategicTargets_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            BSCEntities context = new BSCEntities();

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "StrategicTargetDetails")
            {//create strategic target
                StrategicTargetDetails form = (StrategicTargetDetails)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);


                //int stID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Strategic_Target_ID"]);

                //Strategic_Target st = (from sts in context.Strategic_Target
                //                       where sts.Strategic_Target_ID == stID
                //                       select sts).SingleOrDefault();
                Strategic_Target st = new Strategic_Target();
                st.Name = form.Name;
                st.Description = form.Description;
                st.Perspective_FK = form.Perspective_FK;
                st.ExpectedDate = form.ExpectedDate;

                Strategy_Node sn = new Strategy_Node();
                sn.Positioned = false;
                sn.Strategy_Map_FK = 1; // only one map is used (for now)
                st.Strategy_Node.Add(sn);

                context.AddToStrategic_Target(st);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't create target: " + ex.Message);
                }
            }

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "BusinessSolutionDetails")
            {//create business solution
                BusinessSolutionDetails form = (BusinessSolutionDetails)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);
                
                //int bsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ID"]);

                //Business_Solution bs = (from bss in context.Business_Solution
                //                        where bss.ID == bsID
                //                        select bss).SingleOrDefault();
                int stID = (int)editedItem.OwnerTableView.ParentItem.GetDataKeyValue("Strategic_Target_ID"); // id of parent strategic target

                Business_Solution bs = new Business_Solution();
                bs.Strategic_Target_FK = stID;
                bs.Solution = form.Solution;
                context.AddToBusiness_Solution(bs);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't create solution: " + ex.Message);
                }
            }

            if (editedItem.FindControl(GridEditFormItem.EditFormUserControlID).GetType().BaseType.Name == "KPIDetails_new")
            {//create kpi
                KPIDetails_new form = (KPIDetails_new)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);


                //int kpiID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["KPI_ID"]);

                //KPI kpi = (from kpis in context.KPIs
                //           where kpis.KPI_ID == kpiID
                //           select kpis).SingleOrDefault();
                int bsID = (int)editedItem.OwnerTableView.ParentItem.GetDataKeyValue("ID"); // id of parent business solution

                KPI kpi = new KPI();
                kpi.Solution_FK = bsID;
                kpi.Name = form.Name;
                kpi.Description = form.Description;
                kpi.Perspective_FK = form.Perspective_FK;
                kpi.Measure = form.Measure;

                KPI_Progress kpip = new KPI_Progress();
                kpip.Weight = form.Weight;
                kpip.Min = form.Min;
                kpip.Max = form.Max;
                kpip.Start_Value = form.Start;
                kpip.Target_Value = form.Target;
                kpip.Current_Value = form.Current;
                kpip.Date = DateTime.Today;

                kpip.KPI = kpi;

                //context.AddToKPIs(kpi);
                context.AddToKPI_Progress(kpip);

                try
                {
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new InconsistentDatabaseException("Couldn't create KPI: " + ex.Message);
                }
            }
        }

        protected void RadGridStrategicTargets_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            //StrategicTargetDetails form = (StrategicTargetDetails)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            BSCEntities context = new BSCEntities();
            int stID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Strategic_Target_ID"]);

            Strategic_Target st = (from sts in context.Strategic_Target
                                   where sts.Strategic_Target_ID == stID
                                   select sts).SingleOrDefault();
            context.DeleteObject(st);
            try
            {
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new InconsistentDatabaseException("Couldn't delete target: " + ex.Message);
            }
        }
    }
}